﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Interfaces.Tareas
{
    public partial class BuscarTarea : System.Web.UI.Page
    {
        Model.Programa programatarea { get { return (Model.Programa)ViewState["programatarea"]; } set { ViewState["programatarea"] = value; } }
        protected void Page_Load(object sender, EventArgs e)
        {           
            Label1.Visible = false;
            if (Session["programatarea"] != null)
            {
                RadioButton1.Checked = true;
                RadioButton2.Checked = false;
                Panel2.Visible = RadioButton2.Checked;
                Panel3.Visible = RadioButton1.Checked;
                programatarea = (Model.Programa)Session["programatarea"];
                TextBox8.Text = programatarea.Nombre;
                Session.Remove("programatarea");
            }         
        }

        protected void RadioButton7_CheckedChanged(object sender, EventArgs e)
        {

        }

        protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
        {
            Panel2.Visible = RadioButton2.Checked;
            Panel3.Visible = RadioButton1.Checked;
            DataList1.DataSource = null;
            DataList1.DataBind();
        }

        protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
        {
            Panel2.Visible = RadioButton2.Checked;
            Panel3.Visible = RadioButton1.Checked;
            DataList1.DataSource = null;
            DataList1.DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (RadioButton2.Checked)
                {
                    if (RadioButton3.Checked)
                        DataList1.DataSource = Processes.Tareas.GestorTareas.GetTareas(TextBox1.Text, TextBox4.Text,RadioButton5.Checked,RadioButton6.Checked,RadioButton7.Checked);
                    if (RadioButton4.Checked)
                        DataList1.DataSource = Processes.Tareas.GestorTareas.GetTareasGestiones(TextBox2.Text, TextBox3.Text, RadioButton5.Checked, RadioButton6.Checked, RadioButton7.Checked);
                }
                else
                {
                    if (RadioButton1.Checked)
                    {
                        if (RadioButton8.Checked)
                            DataList1.DataSource = Processes.Tareas.GestorTareas.GestionEspecifica(TextBox8.Text,TextBox5.Text, RadioButton5.Checked, RadioButton6.Checked, RadioButton7.Checked);
                        if (RadioButton9.Checked)
                            DataList1.DataSource = Processes.Tareas.GestorTareas.GetRangoAProgramas(TextBox8.Text, TextBox6.Text,TextBox7.Text, RadioButton5.Checked, RadioButton6.Checked, RadioButton7.Checked);
                    }
                }
                DataList1.DataBind();
                if (DataList1.Items.Count == 0)
                {
                    Label1.Visible = true;
                    Label1.Text = "No se pudo encontrar ninguna Tarea con esas caracteristicas";                   
                }
            }
            catch (Exception ex)
            {
                Label1.Visible = true;
                Label1.Text = ex.Message;
            }
        }

        protected void RadioButton3_CheckedChanged(object sender, EventArgs e)
        {
            Label2.Visible = RadioButton4.Checked;
            Label3.Visible = RadioButton4.Checked;
            TextBox2.Visible = RadioButton4.Checked;
            TextBox3.Visible = RadioButton4.Checked;
            Label4.Visible = RadioButton3.Checked;
            Label5.Visible = RadioButton3.Checked;
            TextBox1.Visible = RadioButton3.Checked;
            TextBox4.Visible = RadioButton3.Checked;
        }

        protected void RadioButton4_CheckedChanged(object sender, EventArgs e)
        {
            Label4.Visible = RadioButton3.Checked;
            Label5.Visible = RadioButton3.Checked;
            TextBox1.Visible = RadioButton3.Checked;
            TextBox4.Visible = RadioButton3.Checked;
            Label2.Visible = RadioButton4.Checked;
            Label3.Visible = RadioButton4.Checked;
            TextBox2.Visible = RadioButton4.Checked;
            TextBox3.Visible = RadioButton4.Checked;
        }

        protected void RadioButton8_CheckedChanged(object sender, EventArgs e)
        {
            Label6.Visible = RadioButton8.Checked;
            TextBox5.Visible = RadioButton8.Checked;
            Label7.Visible = RadioButton9.Checked;
            Label8.Visible = RadioButton9.Checked;
            TextBox6.Visible = RadioButton9.Checked;
            TextBox7.Visible = RadioButton9.Checked;
        }

        protected void RadioButton9_CheckedChanged(object sender, EventArgs e)
        {
            Label6.Visible = RadioButton8.Checked;
            TextBox5.Visible = RadioButton8.Checked;
            Label7.Visible = RadioButton9.Checked;
            Label8.Visible = RadioButton9.Checked;
            TextBox6.Visible = RadioButton9.Checked;
            TextBox7.Visible = RadioButton9.Checked;
        }

        protected void Cerrar_Click(object sender, EventArgs e)
        {

        }

        protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        {
            try
            {
                Label id= (Label)e.Item.FindControl("ID");
                if (id != null)
                {
                    if (e.CommandName == "Cerrar")
                    {
                        Processes.Tareas.GestorTareas.CerrarTarea(id.Text, RadioButton2.Checked, RadioButton1.Checked);
                        LinkButton modificar = (LinkButton)e.Item.FindControl("Modificar");
                        LinkButton eliminar = (LinkButton)e.Item.FindControl("Eliminar");
                        LinkButton cerrar = (LinkButton)e.Item.FindControl("Cerrar");
                        Label estado = (Label)e.Item.FindControl("Estado");
                        modificar.Visible = false;
                        eliminar.Visible = false;
                        cerrar.Visible = false;
                        estado.Text = "Cerrado";
                        Label nombre = (Label)e.Item.FindControl("Nombre");
                        if(RadioButton2.Checked)
                            Response.Redirect("/Tareas/EvaluarTarea.aspx?ID="+id.Text+"&Nm="+nombre.Text+"&R="+false);
                        else
                            Response.Redirect("/Tareas/EvaluarTarea.aspx?ID=" + id.Text + "&Nm=" + nombre.Text + "&R=" + true);
                    }
                    else
                    {
                        if (e.CommandName == "Modificar")
                        {
                            if (RadioButton2.Checked)
                                Response.Redirect("/Tareas/ModificarTarea.aspx?Relacionado=" + false + "&ID=" + id.Text);
                            else
                                Response.Redirect("/Tareas/ModificarTarea.aspx?Relacionado=" + true + "&ID=" + id.Text);
                        }
                        else
                        {
                            if (e.CommandName == "Eliminar")
                            {
                                try
                                {
                                    bool resp = Processes.Tareas.GestorTareas.EliminarTarea(id.Text, RadioButton2.Checked);
                                    if (RadioButton2.Checked)
                                    {
                                        if (RadioButton3.Checked)
                                            DataList1.DataSource = Processes.Tareas.GestorTareas.GetTareas(TextBox1.Text, TextBox4.Text, RadioButton5.Checked, RadioButton6.Checked, RadioButton7.Checked);
                                        if (RadioButton4.Checked)
                                            DataList1.DataSource = Processes.Tareas.GestorTareas.GetTareasGestiones(TextBox2.Text, TextBox3.Text, RadioButton5.Checked, RadioButton6.Checked, RadioButton7.Checked);
                                    }
                                    else
                                    {
                                        if (RadioButton1.Checked)
                                        {
                                            if (RadioButton8.Checked)
                                                DataList1.DataSource = Processes.Tareas.GestorTareas.GestionEspecifica(TextBox8.Text, TextBox5.Text, RadioButton5.Checked, RadioButton6.Checked, RadioButton7.Checked);
                                            if (RadioButton9.Checked)
                                                DataList1.DataSource = Processes.Tareas.GestorTareas.GetRangoAProgramas(TextBox8.Text, TextBox6.Text, TextBox7.Text, RadioButton5.Checked, RadioButton6.Checked, RadioButton7.Checked);
                                        }
                                    }
                                    DataList1.DataBind();
                                    if (DataList1.Items.Count == 0)
                                    {
                                        Label1.Visible = true;
                                        Label1.Text = "No se pudo encontrar ninguna Tarea con esas caracteristicas";
                                    }

                                }
                                catch (Exception ex)
                                {
                                    Label1.Visible = true;
                                    Label1.Text = ex.Message;
                                }
                            }
                            else
                            {
                                if (e.CommandName == "Ver")
                                {
                                    if (RadioButton2.Checked)
                                        Response.Redirect("/Tareas/VerTarea.aspx?Relacionado=" + false + "&ID=" + id.Text);
                                    else
                                        Response.Redirect("/Tareas/VerTarea.aspx?Relacionado=" + true + "&ID=" + id.Text);
                                }
                            }
                        }

                    }
                    }
            }
            catch (Exception ex)
            {
                Label1.Visible = true;
                Label1.Text = ex.Message;
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            //Response.Redirect("/Programa/BuscarPrograma.aspx?Index=" + true + "&Verificar=" + false);  
            Response.Redirect("/Programa/BuscarPrograma.aspx?Index= 1 &Verificar=1");  
        }

        protected void DataList1_SelectedIndexChanged1(object sender, EventArgs e)
        {

        }

        protected void Modificar_Click(object sender, EventArgs e)
        {

        }

        protected void Eliminar_Click(object sender, EventArgs e)
        {

        }

        protected void Ver_Click(object sender, EventArgs e)
        {

        }

       
    }
}